//
//  MapLoader.h
//  Tank For IPhone
//
//  Created by 奕 朱 on 11-3-8.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#include "Constant.h"
#include "StringUtil.h"
#include "Map.h"

@interface MapLoader : NSObject {
	
}

+ (BOOL) loadNextMap;

@end

@implementation MapLoader

+ (BOOL) loadNextMap {
	NSLog(@"load next map");
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	NSString *mapPath = [[NSBundle mainBundle] resourcePath];
	
	mapPath = @"/Users/yuhason/Documents/iphone-workspace/Tank For IPhone/map1.map";
	
    NSError **err; 
	
	NSArray *lines;
	
		
    if([[NSFileManager defaultManager] fileExistsAtPath:mapPath]) {
		lines = [[NSString stringWithContentsOfFile:mapPath 
						encoding: NSUTF8StringEncoding error:err] 
					componentsSeparatedByString:@"\r\n"];
	}
	
	NSMutableArray *_yArrays = [[NSMutableArray alloc] 
									initWithCapacity: [lines count]];
	
	for (int i = 0; i < [lines count]; i ++) {
		NSMutableArray *_xArrays =  [StringUtil splitStr: 
										[lines objectAtIndex:i] 
												cellSize:1];
		[_yArrays addObject: _xArrays];
		
		[_xArrays release];
	}

	
	[[Map alloc] init: _yArrays];
	
	[_yArrays release];
	[lines release];
	[pool release];
	
	return YES;
}

@end

